"use strict";

const { Controller } = require("egg");

class questionController extends Controller {
  async addWithAnswer(){
    const { ctx } = this;
    const req = ctx.request.body;
    const res = await ctx.service.questionService.addWithAnswer(
        req.question,
        req.answerList,
        req.tags,
        req.u_id
    );
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  async del() {
    const { ctx } = this;
    const req = ctx.request.body;
    const res = await ctx.service.questionService.del(req.id, req.u_id);
    const json = ctx.helper.json(
      res.data ? res.data : null,
      res.suc ? 200 : 500,
      res.msg
    );
    ctx.body = json;
  }

  async get() {
    const { ctx } = this;
    const req = ctx.request.body;
    const res = await ctx.service.questionService.get(req);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }
  async search() {
    const { ctx } = this;
    const req = ctx.request.body;
    const res = await ctx.service.questionService.search(req.input);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  async getByTag() {
    const { ctx } = this;
    const req = ctx.request.body;
    const res = await ctx.service.questionService.getByTag(req.tagName);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  async getAll() {
    const { ctx } = this;
    const res = await ctx.service.questionService.getAll();
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }
  async randomGetByTag() {
    const { ctx } = this;
    const req = ctx.request.body;
    const res = await ctx.service.questionService.randomGetByTag(req.count,req.tagName);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }
  async bindExam() {
    const { ctx } = this;
    const req = ctx.request.body;
    const res = await ctx.service.questionService.bindExam(req.e_id,req.questionList);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }
  async unbindExam() {
    const { ctx } = this;
    const req = ctx.request.body;
    const res = await ctx.service.questionService.unbindExam(req.e_id,req.questionList);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }
  async clearExamQuestion() {
    const { ctx } = this;
    const req = ctx.request.body;
    const res = await ctx.service.questionService.bindExam(req.e_id);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }
  async getQuestionByExam() {
    const { ctx } = this;
    const req = ctx.request.body;
    const res = await ctx.service.questionService.getQuestionByExam(req.e_id);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  async updateWithAnswer() {
    const { ctx } = this;
    const req = ctx.request.body;
    const res = await ctx.service.questionService.updateWithAnswer(req.question,req.answerList,req.u_id);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  async addImg() {
    const { ctx } = this;
    const req = ctx.request.body;
    const res = await ctx.service.questionService.addImage(req.src, req.q_id);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  async delImg() {
    const { ctx } = this;
    const req = ctx.request.body;
    const res = await ctx.service.questionService.delImage(req.id);
    ctx.body = ctx.helper.json(
        res.data ? res.data : null,
        res.suc ? 200 : 500,
        res.msg
    );
  }

  async getImg() {
    const { ctx } = this;
    const req = ctx.request.body;
    const res = await ctx.service.questionService.getImage(req.q_id);
    const json = ctx.helper.json(
      res.data ? res.data : null,
      res.suc ? 200 : 500,
      res.msg
    );
    ctx.body = json;
  }

  async updateImg() {
    const { ctx } = this;
    const req = ctx.request.body;

    const res = await ctx.service.questionService.updateImage(req.id,req.src);
    const json = ctx.helper.json(
      res.data ? res.data : null,
      res.suc ? 200 : 500,
      res.msg
    );
    ctx.body = json;
  }


  async addAnswer() {
    const { ctx } = this;
    const req = ctx.request.body;
    const res = await ctx.service.questionService.addAnswer(req.answer, req.q_id);
    const json = ctx.helper.json(
      res.data ? res.data : null,
      res.suc ? 200 : 500,
      res.msg
    );
    ctx.body = json;
  }

  async delAnswer() {
    const { ctx } = this;
    const req = ctx.request.body;
    const res = await ctx.service.questionService.delAnswer(req.id);
    const json = ctx.helper.json(
      res.data ? res.data : null,
      res.suc ? 200 : 500,
      res.msg
    );
    ctx.body = json;
  }

  async getAnswer() {
    const { ctx } = this;
    const req = ctx.request.body;
    const res = await ctx.service.questionService.getAnswer(req.q_id);
    const json = ctx.helper.json(
      res.data ? res.data : null,
      res.suc ? 200 : 500,
      res.msg
    );
    ctx.body = json;
  }

  async updateAnswer() {
    const { ctx } = this;
    const req = ctx.request.body;

    const res = await ctx.service.questionService.updateAnswer(req.id,req.answer);
    const json = ctx.helper.json(
      res.data ? res.data : null,
      res.suc ? 200 : 500,
      res.msg
    );
    ctx.body = json;
  }
}
module.exports = questionController;
